/* ----------------------------------------------------------------------------
* File: quiz_dictionary_impl.cpp
*
* Desc: Implementation for quiz dictionary - definition
*
* Created: 07/05/2011, by Alexey Korshunov
* ---------------------------------------------------------------------------- */
#include "quiz_dictionary_impl.h"
#include "main.h"
#include <fstream>


namespace ModuleManagement
{
	QuizDictionaryManagerImpl::QuizDictionaryManagerImpl(const std::string& FileName)
		throw(Common::Exception)
		: m_QuestionId(0)
	{
		std::fstream file;
		char str[1024];
		char *question = NULL, *answer = NULL;

		// Open file
		MyOutputDebugString("%s", FileName.c_str());
		file.open(FileName.c_str());
		if (!file.is_open())
			throw Common::Exception("skype_plugin: ERROR - Unable to open dictionary file");

		// Read data
		while(file.good())
		{
			file.getline(str, sizeof(str));

			question = strtok(str, "|");
			answer = strtok(NULL, "|");

			if (question != NULL && answer != NULL)
			{
				QuestionPair *onePair = new QuestionPair;
				onePair->question.append(question);
				onePair->answer.append(answer);
				m_Questions.push_back(onePair);
			}
		}
		file.close();
	}

	QuizDictionaryManagerImpl::~QuizDictionaryManagerImpl()
	{
		VectorQuestionsIterator it = m_Questions.begin();
		for (; it != m_Questions.end(); ++it)
			delete *it;
		m_Questions.clear();
	}

	void QuizDictionaryManagerImpl::GetDictionaryInfo(QuizCore::DictionaryInfo& info)
	{
		info.CountQuestions = (unsigned int)m_Questions.size();
	}

	void QuizDictionaryManagerImpl::SetCurNumberQuestion(unsigned int Num)
	{
		m_QuestionId = Num;
	}

	const std::string& QuizDictionaryManagerImpl::GetQuestion()
	{
		QuestionPair *one_pair = m_Questions[m_QuestionId];
		m_Answer = one_pair->answer;
		return one_pair->question;
	}

	const std::string& QuizDictionaryManagerImpl::GetAnswer()
	{
		return m_Answer;
	}
}

